package com.securitytrace.client;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

import com.securitytrace.client.addresslistener.AddressActionListener;
import com.securitytrace.client.addresslistener.AddressListenerController;
import com.securitytrace.client.productlistener.ProductActionListener;
import com.securitytrace.client.productlistener.ProductListenerController;
import com.securitytrace.client.rolelistener.RoleActionListener;
import com.securitytrace.client.rolelistener.RoleListenerController;
import com.securitytrace.client.userlistener.UserActionListener;
import com.securitytrace.client.userlistener.UserListenerController;
import com.securitytrace.dataModel.AddressDataModel;
import com.securitytrace.dataModel.ProductDataModel;
import com.securitytrace.dataModel.RoleDataModel;
import com.securitytrace.dataModel.UserDataModel;
import com.securitytrace.init.InitContext;
import com.securitytrace.model.Address;
import com.securitytrace.model.Product;
import com.securitytrace.model.Role;
import com.securitytrace.model.User;

public class ManagerClient extends ManagerInfoWindow {

	private static final long serialVersionUID = -2388066610900732427L;
	private int pageSize;

	private User resultUser;
	private Role resultRole;
	private Product resultProduct;
	private Address resultAddress;

	private UserDataModel userModel = new UserDataModel();
	private RoleDataModel roleModel = new RoleDataModel();
	private ProductDataModel productModel = new ProductDataModel();
	private AddressDataModel addressModel = new AddressDataModel();

	public ManagerClient() {
	}

	public void start() {
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenWidth = (int) (screenSize.getWidth() / 2);
		int screenHight = (int) (screenSize.getHeight() / 2);
		int width = this.getWidth();
		int hight = this.getHeight();
		this.setLocation(screenWidth - width / 2, screenHight - hight / 2);

		this.initComp();
		this.initListener();
		super.setVisible(true);
	}

	public void initComp() {
		userPanel.userTable.setModel(userModel);
		userPanel.pageInfoLabel.setText(userModel.getUserPageInfo());
		rolePanel.roleInfoTable.setModel(roleModel);
		rolePanel.rolePageInfoLabel.setText(roleModel.getRolePageInfo());
		productPanel.productTable.setModel(productModel);
		productPanel.productPageInfoLabel.setText(productModel
				.getProductPageInfo());
		addressPanel.addressTable.setModel(addressModel);
		addressPanel.addressPageInfoLabel.setText(addressModel
				.getAddressPageInfo());
		this.showTree();
	}

	private void showTree() {
		DefaultMutableTreeNode treeNode1 = new DefaultMutableTreeNode("信息管理列表");
		DefaultMutableTreeNode treeNode2 = new DefaultMutableTreeNode("用户管理");
		treeNode1.add(treeNode2);
		treeNode2 = new DefaultMutableTreeNode("角色管理");
		treeNode1.add(treeNode2);
		treeNode2 = new DefaultMutableTreeNode("产品类别管理");
		treeNode1.add(treeNode2);
		treeNode2 = new DefaultMutableTreeNode("登记点信息管理");
		treeNode1.add(treeNode2);
		mainTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
	}

	public void initListener() {

		mainTree.addTreeSelectionListener(new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent e) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) mainTree
						.getLastSelectedPathComponent();
				System.out.println(node.toString());
				if (node.toString().equals("用户管理")) {
					cardLayout.show(mainCardPanel, "userPanel");
				} else if (node.toString().equals("角色管理")) {
					cardLayout.show(mainCardPanel, "rolePanel");
				} else if (node.toString().equals("产品类别管理")) {
					cardLayout.show(mainCardPanel, "productPanel");
				} else if (node.toString().equals("登记点信息管理")) {
					cardLayout.show(mainCardPanel, "addressPanel");
				}
			}
		});

		this.initUserListener();
		this.initRoleListener();
		this.initProductListener();
		this.initAddressListener();
	}

	private void initUserListener() {
		UserActionListener userActionListener = new UserActionListener();
		userActionListener.setManagerClient(this);
		UserListenerController userListenerController = new UserListenerController(userActionListener);
		
		userPanel.addButton.setActionCommand("addUser");
		userPanel.addButton.addActionListener(userListenerController);
		userPanel.deleteButton.setActionCommand("deleteUser");
		userPanel.deleteButton.addActionListener(userListenerController);
		userPanel.updateButton.setActionCommand("updateUser");
		userPanel.updateButton.addActionListener(userListenerController);
		userPanel.homePageButton.setActionCommand("homePageAction");
		userPanel.homePageButton.addActionListener(userListenerController);
		userPanel.previousPageButton.setActionCommand("previousPageAction");
		userPanel.previousPageButton.addActionListener(userListenerController);
		userPanel.nextPageButton.setActionCommand("nextPageAction");
		userPanel.nextPageButton.addActionListener(userListenerController);
		userPanel.lastPageButton.setActionCommand("lastPageAction");
		userPanel.lastPageButton.addActionListener(userListenerController);
	}

	private void initRoleListener() {

		RoleActionListener roleActionListener = new RoleActionListener();
		roleActionListener.setManagerClient(this);
		RoleListenerController roleListenerController = new RoleListenerController(roleActionListener);
		
		rolePanel.addRoleButton.setActionCommand("addRole");
		rolePanel.addRoleButton.addActionListener(roleListenerController);
		rolePanel.deleteRoleButton.setActionCommand("deleteRole");
		rolePanel.deleteRoleButton.addActionListener(roleListenerController);
		rolePanel.updateRoleButton.setActionCommand("updateRole");
		rolePanel.updateRoleButton.addActionListener(roleListenerController);
		rolePanel.roleHomePageButton.setActionCommand("homePageAction");
		rolePanel.roleHomePageButton.addActionListener(roleListenerController);
		rolePanel.rolePreviousPageButton.setActionCommand("previousPageAction");
		rolePanel.rolePreviousPageButton.addActionListener(roleListenerController);
		rolePanel.roleNnexPagetButton.setActionCommand("nextPageAction");
		rolePanel.roleNnexPagetButton.addActionListener(roleListenerController);
		rolePanel.roleLastPageButton.setActionCommand("lastPageAction");
		rolePanel.roleLastPageButton.addActionListener(roleListenerController);
	}

	private void initProductListener() {
		
		ProductActionListener productActionListener = new ProductActionListener();
		productActionListener.setManagerClient(this);
		ProductListenerController productListenerController = new ProductListenerController(productActionListener);
		
		productPanel.addProductButton.setActionCommand("addProduct");
		productPanel.addProductButton.addActionListener(productListenerController);		
		productPanel.updateProductButton.setActionCommand("updateProduct");
		productPanel.updateProductButton.addActionListener(productListenerController);		
		productPanel.deleteProductButton.setActionCommand("deleteProduct");
		productPanel.deleteProductButton.addActionListener(productListenerController);		
		productPanel.productHomePageButton.setActionCommand("homePageAction");
		productPanel.productHomePageButton.addActionListener(productListenerController);		
		productPanel.productPreviousPageButton.setActionCommand("previousPageAction");
		productPanel.productPreviousPageButton.addActionListener(productListenerController);		
		productPanel.productNextPageButton.setActionCommand("nextPageAction");
		productPanel.productNextPageButton.addActionListener(productListenerController);		
		productPanel.productLastPageButton.setActionCommand("lastPageAction");
		productPanel.productLastPageButton.addActionListener(productListenerController);
		
	}

	private void initAddressListener() {
		
		AddressActionListener addressActionListener = new AddressActionListener();
		addressActionListener.setManagerClient(this);
		AddressListenerController addressListenerController = new AddressListenerController(addressActionListener);
		
		addressPanel.addAddressButton.setActionCommand("addAddress");
		addressPanel.addAddressButton.addActionListener(addressListenerController);
		addressPanel.deleteAddressButton.setActionCommand("deleteAddress");
		addressPanel.deleteAddressButton.addActionListener(addressListenerController);
		addressPanel.updateAddressButton.setActionCommand("updateAddress");
		addressPanel.updateAddressButton.addActionListener(addressListenerController);
		addressPanel.addressHomePageButton.setActionCommand("homePageAction");
		addressPanel.addressHomePageButton.addActionListener(addressListenerController);
		addressPanel.addressPreviousPageButton.setActionCommand("previousPageAction");
		addressPanel.addressPreviousPageButton.addActionListener(addressListenerController);
		addressPanel.addressNextPageButton.setActionCommand("nextPageAction");
		addressPanel.addressNextPageButton.addActionListener(addressListenerController);
		addressPanel.addressLastPageButton.setActionCommand("lastPageAction");
		addressPanel.addressLastPageButton.addActionListener(addressListenerController);
	}

	public static void main(String[] args) {
		int page = 2;
		ManagerClient managerClient = new ManagerClient();
		managerClient.setPageSize(page);
		InitContext context = new InitContext(managerClient);
		context.initDataModel(page);
		managerClient.start();
	}

	/* 以下为get/set方法 */
	public User getResultUser() {
		return resultUser;
	}

	public void setResult(User resultUser) {
		this.resultUser = resultUser;
	}

	public Role getResultRole() {
		return resultRole;
	}

	public void setResultRole(Role resultRole) {
		this.resultRole = resultRole;
	}

	public Product getResultProduct() {
		return resultProduct;
	}

	public void setResultProduct(Product resultProduct) {
		this.resultProduct = resultProduct;
	}

	public Address getResultAddress() {
		return resultAddress;
	}

	public void setResultAddress(Address resultAddress) {
		this.resultAddress = resultAddress;
	}

	public UserDataModel getUserModel() {
		return userModel;
	}

	public void setUserModel(UserDataModel userModel) {
		this.userModel = userModel;
	}

	public RoleDataModel getRoleModel() {
		return roleModel;
	}

	public void setRoleModel(RoleDataModel roleModel) {
		this.roleModel = roleModel;
	}

	public ProductDataModel getProductModel() {
		return productModel;
	}

	public void setProductModel(ProductDataModel productModel) {
		this.productModel = productModel;
	}

	public AddressDataModel getAddressModel() {
		return addressModel;
	}

	public void setAddressModel(AddressDataModel addressModel) {
		this.addressModel = addressModel;
	}

	public int getPageSize() {
		if (pageSize > 0) {
			return pageSize;
		} else {
			return 10;
		}
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

}